﻿using MD5Hash;
using WebMODEL;

namespace WebDAL
{
    public class MemberDAL
    {
        public MemberDAL(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="m"></param>
        /// <returns></returns>
        public int AddMem(Member m)
        {
            var sql = Context.Members.Any(x => x.Name == m.Name);
            if (sql)
            {
                return -1;
            }
            m.Pwd = m.Pwd.GetMD5();
            Context.Members.Add(m);
            return Context.SaveChanges();
        }
       
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="Name"></param>
        /// <param name="Pwd"></param>
        /// <returns></returns>
        public Member Login(string Name, string Pwd)
        { 
            var t=Context.Members.FirstOrDefault(m => m.Name == Name);
            if (t == null)
            {
                return new Member { Id = -1 };
            }
            else
            {
                if (t.Pwd == Pwd.GetMD5())
                {
                    return t;
                }
                else
                {
                    return new Member { Id = -2 };
                }
            }
        }
    }
}
